package com.flea.hclv.controller;

import com.alibaba.fastjson.JSONObject;
import com.flea.hclv.constant.Const;
import com.flea.hclv.model.entity.HGuideAccount;
import com.flea.hclv.service.HGuideAccountService;
import com.flea.hclv.util.ServerResponse;
import com.flea.system.platform.session.FleaOrgUtils;
import com.flea.system.security.ILoginInfo;
import com.flea.system.security.LoginCheck;
import flea.sys.cache.FleaCacheUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * @program: project
 * @description: 导游注册
 * @author: shiliqiang
 * @create: 2018-11-17 20:51
 * @version: 1.0
 */
@Controller
@RequestMapping("/flea/hclv/guide/account/")
public class HGuideAccountController {

    @Autowired
    private HGuideAccountService hGuideAccountService;


    /**
     * 导游注册
     * @return String
     */
    @RequestMapping("register")
    @ResponseBody
    public ServerResponse<String> register(String username,String password,String phone,String email,String question,
    String answer) {
        //用户名 密码 email phone  问题 答案
        try {
			ServerResponse<String> result =  hGuideAccountService.save(username,password,phone,email,question,answer);
			return result;
		} catch (Exception e) {
			return ServerResponse.createByErrorMessage(e.getMessage());
		}
        
        
    }

    @RequestMapping("login")
    @ResponseBody
    public ServerResponse<HGuideAccount> login(String model, HttpServletRequest request) {
    	HttpSession session = request.getSession();
        ServerResponse<HGuideAccount> serverResponse = hGuideAccountService.findByUsernameOrPass(model);
        if (!serverResponse.isSuccess()){
            return serverResponse;
        }
        JSONObject model1 = serverResponse.getModel();
        
    
        
        HGuideAccount hGuideAccount = (HGuideAccount) model1.get("data");
        LoginCheck chk = new LoginCheck();
        chk.setCode(hGuideAccount.getUsername());
        chk.setName(hGuideAccount.getUsername());
        chk.setId(hGuideAccount.getId());
        
        session.setAttribute(ILoginInfo.ID_userInfoInSesion,chk);
        session.setAttribute("user_id",hGuideAccount.getId());
        return serverResponse;
    }

    /**
     * 通过用户名获取问题
     * @param username 用户名
     * @return string
     */
    @RequestMapping("forgotPassword")
    @ResponseBody
    public ServerResponse<Map<String,String>> getQuestion(String username) {
        ServerResponse<Map<String,String>> response =  hGuideAccountService.findQuestionByUsername(username);
        if (response.isSuccess()){
            Map<String,String> map1 = response.getData();
            String username1 = map1.get("username");
//            session.setAttribute(Const.USERNAME,username1);
            FleaCacheUtil.put(Const.USERNAME,username1);
            return response;
        }
        return response;
    }

    /**
     * 提交问题答案
     * @param answer 答案
     * @return string
     */
    @RequestMapping("submitAnswer")
    @ResponseBody
    public ServerResponse<Map<String,String>> getAnswer(String answer) {
        String username = (String) FleaCacheUtil.get(Const.USERNAME);

        if (username==null){
            return ServerResponse.createByErrorMessage("请重新返回操作");
        }
        ServerResponse<Map<String,String>> serverResponse = hGuideAccountService.getAnswer(username,answer);
        return serverResponse;
    }

    @RequestMapping("forgotResetPassword")
    @ResponseBody
    public ServerResponse<Map<String,String>> forgotResetPassword(String password,String username) {
        ServerResponse<Map<String,String>> response = hGuideAccountService.updateResetPassword(username,password);
        if (response.isSuccess()){
            String username1 = (String) FleaCacheUtil.get(Const.USERNAME);
            if (username1!=null){
                FleaCacheUtil.remove(Const.USERNAME);
            }
        }
        return response;
    }

    @RequestMapping("updatePassword")
    @ResponseBody
    public ServerResponse<String> updatePass(HttpServletRequest request,String oldPass, String newPass){
        String userId = FleaOrgUtils.getCurUserUuid(request);
        return hGuideAccountService.updatePassword(userId,oldPass,newPass);
    }


}
